package com.huidz.calendar.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.transition.Slide;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.huidz.calendar.R;
import com.huidz.calendar.util.Apiutils;
import com.huidz.calendar.util.Url;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import pub.devrel.easypermissions.EasyPermissions;

public class UpdateBWLActivity extends Activity implements View.OnClickListener {


    public static int indexs = 0;//用于获取当前的下标，判断完成的是那个页面的值
    private TextView title_comeback;
    private TextView title_return;

    private LinearLayout bwltime_line, bwltx_line, bwlcf_line, bwldd_line, bwlbz_line;
    private TextView bwl_tiem, bwl_chongfu, bwl_tixings;
    public static EditText edit, address, mrak;
    private static PopupWindow popupWindow;
    /******************窗口里控件*******************/
    private RelativeLayout butixing, zhengdian, wufenzhong, shiwufen, sanshifen, yixiaoshi, yitian, santian, wutian;
    private RelativeLayout buchongfu, nian, yue, zhou, tian;
    public static int tixingid, chongfuid;
    private Date gettime;
    public static String gettimestr;
    public String ids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.BOTTOM);
        slide.setDuration(200L);//间歇时间
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.fragment_addbwls);
        inti();
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        Intent getindex = getIntent();
        ids=getindex.getStringExtra("id");
        title_comeback = findViewById(R.id.title_comeback);
        title_comeback.setOnClickListener(this);
        title_return = findViewById(R.id.title_return);
        title_return.setOnClickListener(this);
        bwltime_line = findViewById(R.id.bwltime_line);
        bwltime_line.setOnClickListener(this);
        bwltx_line = findViewById(R.id.bwltx_line);
        bwltx_line.setOnClickListener(this);
        bwlcf_line = findViewById(R.id.bwlcf_line);
        bwlcf_line.setOnClickListener(this);
        bwldd_line = findViewById(R.id.bwldd_line);
        bwldd_line.setOnClickListener(this);
        bwlbz_line = findViewById(R.id.bwlbz_line);
        bwlbz_line.setOnClickListener(this);
        bwl_tiem = findViewById(R.id.bwl_tiem);
        bwl_chongfu = findViewById(R.id.bwl_chongfu);
        bwl_tixings = findViewById(R.id.bwl_tixings);
        address = findViewById(R.id.address);
        mrak = findViewById(R.id.mrak);
        edit = findViewById(R.id.edit);
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edit.setFocusable(true);
                edit.setFocusableInTouchMode(true);
                edit.requestFocus();
                if (!isSoftShowing()) {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        });
        DateTimePpicke(UpdateBWLActivity.this);
        gettime();
    }

    public void gettime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");
        gettime = new Date(System.currentTimeMillis());
        gettimestr = formatter.format(gettime);
    }


    public void lost() {
        edit.setFocusable(false);
        edit.setFocusableInTouchMode(false);
        edit.requestFocus();
        if (isSoftShowing()) {
            final View v = getWindow().peekDecorView();
            if (v != null && v.getWindowToken() != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }

    View popupwindowview;
    private ImageView buchongfu_check_yes, nian_check_yes, yue_check_yes, zhou_check_yes, tian_check_yes;
    private ImageView butixing_check_yes, zhengdian_check_yes, wufenzhong_check_yes, shiwufen_check_yes, sanshifen_check_yes, yixiaoshi_check_yes, yitian_check_yes, santian_check_yes, wutian_check_yes;
    private int check_id = 0, check_tixing_id = 1;


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    public void window(Context context, int post) {
        if (post == 0) {
            popupwindowview = LayoutInflater.from(context).inflate(
                    R.layout.activity_tqwindow, null);
            butixing = popupwindowview.findViewById(R.id.butixing);
            zhengdian = popupwindowview.findViewById(R.id.zhengdian);
            wufenzhong = popupwindowview.findViewById(R.id.wufenzhong);
            shiwufen = popupwindowview.findViewById(R.id.shiwufen);
            sanshifen = popupwindowview.findViewById(R.id.sanshifen);
            yixiaoshi = popupwindowview.findViewById(R.id.yixiaoshi);
            yitian = popupwindowview.findViewById(R.id.yitian);
            santian = popupwindowview.findViewById(R.id.santian);
            wutian = popupwindowview.findViewById(R.id.wutian);
            butixing_check_yes = popupwindowview.findViewById(R.id.butixing_check_yes);
            zhengdian_check_yes = popupwindowview.findViewById(R.id.zhengdian_check_yes);
            wufenzhong_check_yes = popupwindowview.findViewById(R.id.wufenzhong_check_yes);
            shiwufen_check_yes = popupwindowview.findViewById(R.id.shiwufen_check_yes);
            sanshifen_check_yes = popupwindowview.findViewById(R.id.sanshifen_check_yes);
            yixiaoshi_check_yes = popupwindowview.findViewById(R.id.yixiaoshi_check_yes);
            yitian_check_yes = popupwindowview.findViewById(R.id.yitian_check_yes);
            santian_check_yes = popupwindowview.findViewById(R.id.santian_check_yes);
            wutian_check_yes = popupwindowview.findViewById(R.id.wutian_check_yes);
            Check_ChongFu(check_tixing_id, post);
            butixing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    check_tixing_id = 0;
                    tixingid = 0;
                    popupWindow.dismiss();
                    bwl_tixings.setText("不提醒");
                }
            });
            zhengdian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tixingid = 1;
                    check_tixing_id = 1;
                    popupWindow.dismiss();
                    bwl_tixings.setText("正点提醒");
                }
            });
            wufenzhong.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tixingid = 2;
                    check_tixing_id = 2;
                    popupWindow.dismiss();
                    bwl_tixings.setText("提前5分钟");
                }
            });
            shiwufen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    check_tixing_id = 3;
                    tixingid = 3;
                    popupWindow.dismiss();
                    bwl_tixings.setText("提前15分钟");
                }
            });
            sanshifen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tixingid = 4;
                    check_tixing_id = 4;
                    popupWindow.dismiss();
                    bwl_tixings.setText("提前30分钟");
                }
            });
            yixiaoshi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tixingid = 5;
                    check_tixing_id = 5;
                    popupWindow.dismiss();
                    bwl_tixings.setText("提前一小时");
                }
            });
            yitian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tixingid = 6;
                    check_tixing_id = 6;
                    popupWindow.dismiss();
                    bwl_tixings.setText("提前1天");
                }
            });
            santian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tixingid = 7;
                    check_tixing_id = 7;
                    popupWindow.dismiss();
                    bwl_tixings.setText("提前3天");
                }
            });
            wutian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tixingid = 8;
                    check_tixing_id = 8;
                    popupWindow.dismiss();
                    bwl_tixings.setText("提前5天");
                }
            });
        } else {
            popupwindowview = LayoutInflater.from(context).inflate(
                    R.layout.activity_cfwindow, null);
            buchongfu = popupwindowview.findViewById(R.id.buchongfu);
            nian = popupwindowview.findViewById(R.id.nian);
            yue = popupwindowview.findViewById(R.id.yue);
            zhou = popupwindowview.findViewById(R.id.zhou);
            tian = popupwindowview.findViewById(R.id.tian);
            buchongfu_check_yes = popupwindowview.findViewById(R.id.buchongfu_check_yes);
            nian_check_yes = popupwindowview.findViewById(R.id.nian_check_yes);
            yue_check_yes = popupwindowview.findViewById(R.id.yue_check_yes);
            zhou_check_yes = popupwindowview.findViewById(R.id.zhou_check_yes);
            tian_check_yes = popupwindowview.findViewById(R.id.tian_check_yes);
            Check_ChongFu(check_id, post);
            buchongfu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bwl_chongfu.setText("不重复");
                    chongfuid = 0;
                    check_id = 0;
                    popupWindow.dismiss();
                }
            });
            nian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bwl_chongfu.setText("按年重复");
                    chongfuid = 4;
                    check_id = 1;
                    popupWindow.dismiss();
                }
            });
            yue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bwl_chongfu.setText("按月重复");
                    chongfuid = 3;
                    check_id = 2;
                    popupWindow.dismiss();
                }
            });
            zhou.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bwl_chongfu.setText("按周重复");
                    chongfuid = 2;
                    check_id = 3;
                    popupWindow.dismiss();
                }
            });
            tian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bwl_chongfu.setText("按天重复");
                    chongfuid = 1;
                    check_id = 4;
                    popupWindow.dismiss();
                }
            });
        }
        popupWindow = new PopupWindow(popupwindowview, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.setTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        popupWindow.showAtLocation(bwltx_line, Gravity.BOTTOM, 0, 0);
    }

    TimePickerView pvTime;
    private String txtime;
    public static String datetime_s = "";

    public void DateTimePpicke(Context context) {
        Calendar selectedDate = Calendar.getInstance();
        Time t = new Time();
        t.setToNow(); // 取得系统时间。
        int year = t.year;
        int month = t.month;
        int day = t.monthDay;
        int mWay = selectedDate.get(Calendar.DAY_OF_WEEK);
        String mway = null;
        mway = Apiutils.GetWeekday(mWay);
        int h = t.hour;
        int m = t.minute;
        bwl_tiem.setText(year + "年" + (month + 1) + "月" + day + "日" + " 周" + mway + " " + h + ":" + m);
        datetime_s = date2TimeStamp(year + "年" + (month + 1) + "月" + day + "日");
        Apiutils.SetLog("datetime_s:" + datetime_s);
        selectedDate.set(year, month, day);
        pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH) + 1;
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                int mway = calendar.get(Calendar.DAY_OF_WEEK);
                int h = calendar.get(Calendar.HOUR_OF_DAY);
                bwl_tiem.setText(year + "年" + month + "月" + day + "日" + " 周" + Apiutils.GetWeekday(mway) + " " + h + ":" + calendar.get(Calendar.MINUTE));
                gettimestr = year + "年" + month + "月" + day + "日" + " " + h + ":" + calendar.get(Calendar.MINUTE);
                datetime_s = date2TimeStamp(year + "年" + month + "月" + day + "日");
                if (month > 9 && day > 9)
                    txtime = year + "" + month + "" + day + "" + h + "" + calendar.get(Calendar.MINUTE) + "";
                else if (month <= 9 && day > 9)
                    txtime = year + "0" + month + "" + day + "" + h + "" + calendar.get(Calendar.MINUTE) + "";
                else if (month > 9 && day <= 9)
                    txtime = year + "" + month + "0" + day + "" + h + "" + calendar.get(Calendar.MINUTE) + "";
                else
                    txtime = year + "0" + month + "0" + day + "" + h + "" + calendar.get(Calendar.MINUTE) + "";
            }
        }).setDate(selectedDate)
                .setSubmitColor(R.color.text_msg)
                .setCancelColor(R.color.text_msg)
                .setOutSideCancelable(false)
                .setType(new boolean[]{true, true, true, true, true, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .build();
    }

    private boolean isSoftShowing() {
        //获取当屏幕内容的高度
        int screenHeight = getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        //DecorView即为activity的顶级view
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        //考虑到虚拟导航栏的情况（虚拟导航栏情况下：screenHeight = rect.bottom + 虚拟导航栏高度）
        //选取screenHeight*2/3进行判断
        return screenHeight * 2 / 3 > rect.bottom;
    }


    public void Check_ChongFu(int post, int popupwindowid) {
        if (popupwindowid == 1)
            switch (post) {
                case 0:
                    buchongfu_check_yes.setVisibility(View.VISIBLE);
                    nian_check_yes.setVisibility(View.GONE);
                    yue_check_yes.setVisibility(View.GONE);
                    zhou_check_yes.setVisibility(View.GONE);
                    tian_check_yes.setVisibility(View.GONE);
                    buchongfu.setBackgroundResource(R.drawable.rela_tixing_check);
                    nian.setBackgroundResource(R.drawable.rela_tixing);
                    yue.setBackgroundResource(R.drawable.rela_tixing);
                    zhou.setBackgroundResource(R.drawable.rela_tixing);
                    tian.setBackgroundResource(R.drawable.rela_tixing);
                    break;
                case 1:
                    buchongfu_check_yes.setVisibility(View.GONE);
                    nian_check_yes.setVisibility(View.VISIBLE);
                    yue_check_yes.setVisibility(View.GONE);
                    zhou_check_yes.setVisibility(View.GONE);
                    tian_check_yes.setVisibility(View.GONE);
                    buchongfu.setBackgroundResource(R.drawable.rela_tixing);
                    nian.setBackgroundResource(R.drawable.rela_tixing_check);
                    yue.setBackgroundResource(R.drawable.rela_tixing);
                    zhou.setBackgroundResource(R.drawable.rela_tixing);
                    tian.setBackgroundResource(R.drawable.rela_tixing);
                    break;
                case 2:
                    buchongfu_check_yes.setVisibility(View.GONE);
                    nian_check_yes.setVisibility(View.GONE);
                    yue_check_yes.setVisibility(View.VISIBLE);
                    zhou_check_yes.setVisibility(View.GONE);
                    tian_check_yes.setVisibility(View.GONE);
                    buchongfu.setBackgroundResource(R.drawable.rela_tixing);
                    nian.setBackgroundResource(R.drawable.rela_tixing);
                    yue.setBackgroundResource(R.drawable.rela_tixing_check);
                    zhou.setBackgroundResource(R.drawable.rela_tixing);
                    tian.setBackgroundResource(R.drawable.rela_tixing);
                    break;
                case 3:
                    buchongfu_check_yes.setVisibility(View.GONE);
                    nian_check_yes.setVisibility(View.GONE);
                    yue_check_yes.setVisibility(View.GONE);
                    zhou_check_yes.setVisibility(View.VISIBLE);
                    tian_check_yes.setVisibility(View.GONE);
                    buchongfu.setBackgroundResource(R.drawable.rela_tixing);
                    nian.setBackgroundResource(R.drawable.rela_tixing);
                    yue.setBackgroundResource(R.drawable.rela_tixing);
                    zhou.setBackgroundResource(R.drawable.rela_tixing_check);
                    tian.setBackgroundResource(R.drawable.rela_tixing);
                    break;
                case 4:
                    buchongfu_check_yes.setVisibility(View.GONE);
                    nian_check_yes.setVisibility(View.GONE);
                    yue_check_yes.setVisibility(View.GONE);
                    zhou_check_yes.setVisibility(View.GONE);
                    tian_check_yes.setVisibility(View.VISIBLE);
                    buchongfu.setBackgroundResource(R.drawable.rela_tixing);
                    nian.setBackgroundResource(R.drawable.rela_tixing);
                    yue.setBackgroundResource(R.drawable.rela_tixing);
                    zhou.setBackgroundResource(R.drawable.rela_tixing);
                    tian.setBackgroundResource(R.drawable.rela_tixing_check);
                    break;
            }
        else
            switch (post) {
                case 0:
                    butixing.setBackgroundResource(R.drawable.rela_tixing_check);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing);
                    yitian.setBackgroundResource(R.drawable.rela_tixing);
                    santian.setBackgroundResource(R.drawable.rela_tixing);
                    wutian.setBackgroundResource(R.drawable.rela_tixing);
                    butixing_check_yes.setVisibility(View.VISIBLE);
                    zhengdian_check_yes.setVisibility(View.GONE);
                    wufenzhong_check_yes.setVisibility(View.GONE);
                    shiwufen_check_yes.setVisibility(View.GONE);
                    sanshifen_check_yes.setVisibility(View.GONE);
                    yixiaoshi_check_yes.setVisibility(View.GONE);
                    yitian_check_yes.setVisibility(View.GONE);
                    santian_check_yes.setVisibility(View.GONE);
                    wutian_check_yes.setVisibility(View.GONE);
                    break;
                case 1:
                    butixing.setBackgroundResource(R.drawable.rela_tixing);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing_check);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing);
                    yitian.setBackgroundResource(R.drawable.rela_tixing);
                    santian.setBackgroundResource(R.drawable.rela_tixing);
                    wutian.setBackgroundResource(R.drawable.rela_tixing);
                    butixing_check_yes.setVisibility(View.GONE);
                    zhengdian_check_yes.setVisibility(View.VISIBLE);
                    wufenzhong_check_yes.setVisibility(View.GONE);
                    shiwufen_check_yes.setVisibility(View.GONE);
                    sanshifen_check_yes.setVisibility(View.GONE);
                    yixiaoshi_check_yes.setVisibility(View.GONE);
                    yitian_check_yes.setVisibility(View.GONE);
                    santian_check_yes.setVisibility(View.GONE);
                    wutian_check_yes.setVisibility(View.GONE);
                    break;
                case 2:
                    butixing.setBackgroundResource(R.drawable.rela_tixing);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing_check);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing);
                    yitian.setBackgroundResource(R.drawable.rela_tixing);
                    santian.setBackgroundResource(R.drawable.rela_tixing);
                    wutian.setBackgroundResource(R.drawable.rela_tixing);
                    butixing_check_yes.setVisibility(View.GONE);
                    zhengdian_check_yes.setVisibility(View.GONE);
                    wufenzhong_check_yes.setVisibility(View.VISIBLE);
                    shiwufen_check_yes.setVisibility(View.GONE);
                    sanshifen_check_yes.setVisibility(View.GONE);
                    yixiaoshi_check_yes.setVisibility(View.GONE);
                    yitian_check_yes.setVisibility(View.GONE);
                    santian_check_yes.setVisibility(View.GONE);
                    wutian_check_yes.setVisibility(View.GONE);
                    break;
                case 3:
                    butixing.setBackgroundResource(R.drawable.rela_tixing);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing_check);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing);
                    yitian.setBackgroundResource(R.drawable.rela_tixing);
                    santian.setBackgroundResource(R.drawable.rela_tixing);
                    wutian.setBackgroundResource(R.drawable.rela_tixing);
                    butixing_check_yes.setVisibility(View.GONE);
                    zhengdian_check_yes.setVisibility(View.GONE);
                    wufenzhong_check_yes.setVisibility(View.GONE);
                    shiwufen_check_yes.setVisibility(View.VISIBLE);
                    sanshifen_check_yes.setVisibility(View.GONE);
                    yixiaoshi_check_yes.setVisibility(View.GONE);
                    yitian_check_yes.setVisibility(View.GONE);
                    santian_check_yes.setVisibility(View.GONE);
                    wutian_check_yes.setVisibility(View.GONE);
                    break;
                case 4:
                    butixing.setBackgroundResource(R.drawable.rela_tixing);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing_check);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing);
                    yitian.setBackgroundResource(R.drawable.rela_tixing);
                    santian.setBackgroundResource(R.drawable.rela_tixing);
                    wutian.setBackgroundResource(R.drawable.rela_tixing);
                    butixing_check_yes.setVisibility(View.GONE);
                    zhengdian_check_yes.setVisibility(View.GONE);
                    wufenzhong_check_yes.setVisibility(View.GONE);
                    shiwufen_check_yes.setVisibility(View.GONE);
                    sanshifen_check_yes.setVisibility(View.VISIBLE);
                    yixiaoshi_check_yes.setVisibility(View.GONE);
                    yitian_check_yes.setVisibility(View.GONE);
                    santian_check_yes.setVisibility(View.GONE);
                    wutian_check_yes.setVisibility(View.GONE);
                    break;
                case 5:
                    butixing.setBackgroundResource(R.drawable.rela_tixing);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing_check);
                    yitian.setBackgroundResource(R.drawable.rela_tixing);
                    santian.setBackgroundResource(R.drawable.rela_tixing);
                    wutian.setBackgroundResource(R.drawable.rela_tixing);
                    butixing_check_yes.setVisibility(View.GONE);
                    zhengdian_check_yes.setVisibility(View.GONE);
                    wufenzhong_check_yes.setVisibility(View.GONE);
                    shiwufen_check_yes.setVisibility(View.GONE);
                    sanshifen_check_yes.setVisibility(View.GONE);
                    yixiaoshi_check_yes.setVisibility(View.VISIBLE);
                    yitian_check_yes.setVisibility(View.GONE);
                    santian_check_yes.setVisibility(View.GONE);
                    wutian_check_yes.setVisibility(View.GONE);
                    break;
                case 6:
                    butixing.setBackgroundResource(R.drawable.rela_tixing);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing);
                    yitian.setBackgroundResource(R.drawable.rela_tixing_check);
                    santian.setBackgroundResource(R.drawable.rela_tixing);
                    wutian.setBackgroundResource(R.drawable.rela_tixing);
                    butixing_check_yes.setVisibility(View.GONE);
                    zhengdian_check_yes.setVisibility(View.GONE);
                    wufenzhong_check_yes.setVisibility(View.GONE);
                    shiwufen_check_yes.setVisibility(View.GONE);
                    sanshifen_check_yes.setVisibility(View.GONE);
                    yixiaoshi_check_yes.setVisibility(View.GONE);
                    yitian_check_yes.setVisibility(View.VISIBLE);
                    santian_check_yes.setVisibility(View.GONE);
                    wutian_check_yes.setVisibility(View.GONE);
                    break;
                case 7:
                    butixing.setBackgroundResource(R.drawable.rela_tixing);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing);
                    yitian.setBackgroundResource(R.drawable.rela_tixing);
                    santian.setBackgroundResource(R.drawable.rela_tixing_check);
                    wutian.setBackgroundResource(R.drawable.rela_tixing);
                    butixing_check_yes.setVisibility(View.GONE);
                    zhengdian_check_yes.setVisibility(View.GONE);
                    wufenzhong_check_yes.setVisibility(View.GONE);
                    shiwufen_check_yes.setVisibility(View.GONE);
                    sanshifen_check_yes.setVisibility(View.GONE);
                    yixiaoshi_check_yes.setVisibility(View.GONE);
                    yitian_check_yes.setVisibility(View.GONE);
                    santian_check_yes.setVisibility(View.VISIBLE);
                    wutian_check_yes.setVisibility(View.GONE);
                    break;
                case 8:
                    butixing.setBackgroundResource(R.drawable.rela_tixing);
                    zhengdian.setBackgroundResource(R.drawable.rela_tixing);
                    wufenzhong.setBackgroundResource(R.drawable.rela_tixing);
                    shiwufen.setBackgroundResource(R.drawable.rela_tixing);
                    sanshifen.setBackgroundResource(R.drawable.rela_tixing);
                    yixiaoshi.setBackgroundResource(R.drawable.rela_tixing);
                    yitian.setBackgroundResource(R.drawable.rela_tixing);
                    santian.setBackgroundResource(R.drawable.rela_tixing);
                    wutian.setBackgroundResource(R.drawable.rela_tixing_check);
                    butixing_check_yes.setVisibility(View.GONE);
                    zhengdian_check_yes.setVisibility(View.GONE);
                    wufenzhong_check_yes.setVisibility(View.GONE);
                    shiwufen_check_yes.setVisibility(View.GONE);
                    sanshifen_check_yes.setVisibility(View.GONE);
                    yixiaoshi_check_yes.setVisibility(View.GONE);
                    yitian_check_yes.setVisibility(View.GONE);
                    santian_check_yes.setVisibility(View.GONE);
                    wutian_check_yes.setVisibility(View.VISIBLE);
                    break;
            }
    }


    public static String date2TimeStamp(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            return String.valueOf(sdf.parse(date).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_return:
                if (edit.getText().toString().equals("")) {
                    Apiutils.SetToast(UpdateBWLActivity.this, "请填写提醒内容");
                } else
                    okHttp_postFromParameters(ids, chongfuid + "",datetime_s, "", edit.getText().toString(), "", mrak.getText().toString(), address.getText().toString());
                break;
            case R.id.bwltime_line:
                lost();
                pvTime.show();
                break;
            case R.id.bwltx_line:
                lost();
                window(UpdateBWLActivity.this, 0);
                break;
            case R.id.bwlcf_line:
                lost();
                window(UpdateBWLActivity.this, 1);
                break;
            case R.id.bwldd_line:
                lost();
                break;
            case R.id.bwlbz_line:
                lost();
                break;
            case R.id.title_comeback:
                onBackPressed();
                break;
        }
    }

    public void okHttp_postFromParameters(String type, String repeatetype, String remindtime, String birthdayperson, String memcontent, String commem, String mark, String address) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    RequestBody formBody = new FormBody.Builder().add("logintoken", Url.USER_TOKEN).add("remindid", type).add("repeatetype", repeatetype).add("remindtime", remindtime).add("birthdayperson", birthdayperson).add("memcontent", memcontent).add("commem", commem).add("mark", mark).add("address", address)
                            .build();
                    Request request = new Request.Builder().url(Url.HTTP+"api.xuekaixin.cn/remind/remindupdate.html").post(formBody).build();
                    okhttp3.Response response = okHttpClient.newCall(request).execute();
                    String msg = response.body().string();
                    Message message = new Message();
                    message.obj = msg;
                    gethots.sendMessage(message);
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public Handler gethots = new Handler() {
        public void handleMessage(Message msg) {
            String msgs = null;
            try {
                JSONObject success_Object = null;
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    msgs = success_Object.getString("msg");
                    if (code.equals("1")) {
                        onBackPressed();
                        Apiutils.SetToast(UpdateBWLActivity.this, msgs + "");
                    } else
                        Apiutils.SetToast(UpdateBWLActivity.this, msgs + "");
                }

            } catch (
                    Exception e) {
                e.printStackTrace();
            }
        }
    };
}
